package actions.visitors;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import veilingDomein.User;
import veilingService.DBController;
import veilingService.IUserService;
import veilingService.ServiceProvider;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class MaakAccount extends ActionSupport{

	private String naam;
	private String adres;
	private String Woonplaats;
	private int rechten;
	private String username;
	private String password;
	private String email;
	private IUserService ius = ServiceProvider.getUserService();
	private String passwordRepeat;
	private String emailRepeat;
	private int userId;
	DBController db = new DBController();

	public String execute() {
		//Krediet kr = new Krediet();
		try {
			User user = new User();

			user.setUsername(getUsername());
			user.setNaam(getNaam());
			user.setPassword(getPassword());
			user.setAdres(getAdres());
			user.setEmail(getEmail());
			user.setWoonplaats(getWoonplaats());
			user.setId(ServiceProvider.getUserService().getUserId());
			ius.createUser(user);
			return SUCCESS;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return INPUT;

	}
	public String getNaam() {
		return naam;
	}

	public void setNaam(String naam) {
		this.naam = naam;
	}

	public String getAdres() {
		return adres;
	}

	public void setAdres(String adres) {
		this.adres = adres;
	}

	public String getWoonplaats() {
		return Woonplaats;
	}

	public void setWoonplaats(String woonplaats) {
		Woonplaats = woonplaats;
	}

	public int getRechten() {
		return rechten;
	}

	public void setRechten(int rechten) {
		this.rechten = rechten;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void validate() {
		username = username.trim();
		password = password.trim();


		Connection con = db.connect();

		if (naam.length() == 0 ){			
			addFieldError( "naam", "Vul uw gegevens a.u.b goed in");
		}  else{

			try {
				PreparedStatement pstmt = con
						.prepareStatement("SELECT * FROM GEBRUIKER  WHERE GEBRUIKERSNAAM = '"
								+ username + "'");
				ResultSet rs = pstmt.executeQuery();
				while (rs.next()) {
					addFieldError("username",
							"Gebuikers naam bestaat al, Kies een andere!.");
				}
			} catch (SQLException e) {
			}
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (adres.length() == 0 ){			
			addFieldError( "adres", getText( "adres.required"));
		}

		else if (Woonplaats.length() == 0 ){			
			addFieldError( "woonplaats", getText( "woonplaats.required"));
		}
		else if (username.length() == 0 ){			
			addFieldError( "username", getText("username.required"));
		}

		else if ( password.length() == 0 ){			
			addFieldError( "password", getText("password.required"));
		}
		else if ( password.length() < 8 ){			
			addFieldError( "password", "wachtwoord moet minimaal 8 tekens zijn");
		}
		else if (!password.matches(".*[0-9].*")){
			addFieldError("password", "wachtwoord moet minimaal een cijfer bevatten!");
		}
		else if (!password.equals(passwordRepeat)){
			addFieldError("passwordRepeat", "De wachtwoorden die u invoerde komen niet overeen!");
		}
		else if (email.length() == 0 ){			
			addFieldError( "email", getText("email.required"));
		}
		else if (!email.equals(emailRepeat)){			
			addFieldError( "email", "De e-mailadressen die u invoerde komen niet overeen!");
		}
	}
	public String getPasswordRepeat() {
		return passwordRepeat;
	}
	public void setPasswordRepeat(String passwordRepeat) {
		this.passwordRepeat = passwordRepeat;
	}
	public String getEmailRepeat() {
		return emailRepeat;
	}
	public void setEmailRepeat(String emailRepeat) {
		this.emailRepeat = emailRepeat;
	}

}
